import axios from 'axios'
import { host_custom } from "@/host.js";
import { showToast } from "@/components/toasts/LoginRegToast.js";

const requests  = axios.create({
    baseURL: host_custom,
    timeout:5000,
})

requests.interceptors.request.use((config)=>{
    return config;
})
requests.defaults.withCredentials = true

requests.interceptors.response.use(
    response => response,
    error => {
        const { response } = error
        if (response) {
            const { status } = response
            if (status === 401) {
                showToast('error','账号不存在或密码错误')
            } else if (status === 403) {
                showToast('error','没有权限，请联系管理员')
            } else if (status === 413) {
                showToast('warning','请求实体过大！你在搞什么？')
            } else if (status >= 500) {
                showToast('error',response.data?.msg || '服务器内部错误，请稍后重试')
            } else {
                showToast('error',response.data?.msg || '请求出错')
            }
        } else {
            showToast('error', '请求超时')
        }
        return Promise.reject(error)
    }
)

export default requests